/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.OrderWarePo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  OrderWarePoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class OrderWareSqlProvider {
	public static final String TABLE_NAME = "order_ware";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param orderWare
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderWares(final OrderWarePo orderWare) {
		return new CustomerSQL() {{
			SELECT("amount_total,create_time,creator_id,id,image_main,number,order_id,price_original,price_promotional,update_time,updater_id,ware_id,ware_name, score_total, score, score_promote_total, score_promote, remark ");
			FROM(TABLE_NAME);
			
			if (null != orderWare.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (null != orderWare.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderWare.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != orderWare.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderWare.getWareName())) {
				WHERE("ware_name=#{wareName}");
			}
			if(StringUtils.isNotBlank(orderWare.getSortField())) {
				ORDER_BY(orderWare.getSortField() + (StringUtils.isNotBlank(orderWare.getSortMethod()) ? "desc" : orderWare.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != orderWare.getStart() && null != orderWare.getSize()) {
				LIMIT(orderWare.getStart(), orderWare.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param orderWare
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderWareCount(final OrderWarePo orderWare) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);

			if (null != orderWare.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (null != orderWare.getOrderId()) {
				WHERE("order_id=#{orderId}");
			}
			if (null != orderWare.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != orderWare.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderWare.getWareName())) {
				WHERE("ware_name=#{wareName}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderWareById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param orderWare
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addOrderWare(final OrderWarePo orderWare) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != orderWare.getAmountTotal()) {
				VALUES("amount_total","#{amountTotal}");
			}
			if (null != orderWare.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(orderWare.getImageMain())) {
				VALUES("image_main","#{imageMain}");
			}
			if (null != orderWare.getNumber()) {
				VALUES("number","#{number}");
			}
			if (null != orderWare.getOrderId()) {
				VALUES("order_id","#{orderId}");
			}
			if (null != orderWare.getPriceOriginal()) {
				VALUES("price_original","#{priceOriginal}");
			}
			if (null != orderWare.getPricePromotional()) {
				VALUES("price_promotional","#{pricePromotional}");
			}
			if (null != orderWare.getScorePromote()) {
				VALUES("score_promote","#{scorePromote}");
			}
			if (null != orderWare.getScorePromoteTotal()) {
				VALUES("score_promote_total","#{scorePromoteTotal}");
			}
			if (null != orderWare.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != orderWare.getWareId()) {
				VALUES("ware_id","#{wareId}");
			}
			if (StringUtils.isNotBlank(orderWare.getWareName())) {
				VALUES("ware_name","#{wareName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param orderWare
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateOrderWare(final OrderWarePo orderWare) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != orderWare.getAmountTotal()) {
				SET("amount_total=#{amountTotal}");
			}
			if (StringUtils.isNotBlank(orderWare.getImageMain())) {
				SET("image_main=#{imageMain}");
			}
			if (null != orderWare.getNumber()) {
				SET("number=#{number}");
			}
			if (null != orderWare.getOrderId()) {
				SET("order_id=#{orderId}");
			}
			if (null != orderWare.getPriceOriginal()) {
				SET("price_original=#{priceOriginal}");
			}
			if (null != orderWare.getPricePromotional()) {
				SET("price_promotional=#{pricePromotional}");
			}
			if (null != orderWare.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != orderWare.getWareId()) {
				SET("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderWare.getWareName())) {
				SET("ware_name=#{wareName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delOrderWare(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 批量增加订单商品信息
	 * 
	 * @param map	
	 * @Author 迷蝶
	 * @return String
	 */
	public String insertBatch(final Map<String, List<OrderWarePo>> map) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        sb.append("INSERT INTO ").append(TABLE_NAME).append(" (amount_total,creator_id,"
        		+ "image_main,number,order_id,price_original,price_promotional,updater_id,ware_id,ware_name, score_total, score,score_promote_total, score_promote, remark) VALUES ");
        for (OrderWarePo orderWare : map.get("orderWares")) {
        	if(i > 0) {sb.append(",");}
        	i++;
        	sb.append("(").append(orderWare.getAmountTotal()).append(",")
        	.append(orderWare.getCreatorId()).append(",'")
        	.append(orderWare.getImageMain()).append("',")
        	.append(orderWare.getNumber()).append(",")
        	.append(orderWare.getOrderId()).append(",")
        	.append(orderWare.getPriceOriginal()).append(",")
        	.append(orderWare.getPricePromotional()).append(",")
        	.append(orderWare.getUpdaterId()).append(",")
        	.append(orderWare.getWareId()).append(",'")
        	.append(orderWare.getWareName()).append("',")
        	.append(orderWare.getScoreTotal()).append(",")
        	.append(orderWare.getScore()).append(",")
        	.append(orderWare.getScorePromoteTotal()).append(",")
        	.append(orderWare.getScorePromote()).append(",")
        	.append(null == orderWare.getRemark() ? null : "'" + orderWare.getRemark() + "'")
        	.append(")");
        }
        return sb.toString();
	}
}
